package com.ruoyi.web.controller.system;

import java.util.ArrayList;
import java.util.List;

import com.ruoyi.common.utils.ShiroUtils;
import com.ruoyi.system.domain.Category;
import com.ruoyi.system.domain.vo.ProductVo;
import com.ruoyi.system.service.ICategoryService;
import com.ruoyi.system.service.ISysRoleService;
import com.ruoyi.system.service.ISysUserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.AProduct;
import com.ruoyi.system.service.IAProductService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

import javax.annotation.Resource;

import static com.ruoyi.common.core.domain.entity.SysUser.isAdmin;


/**
 * 商品Controller
 * 
 * @author 张颖
 * @date 2025-01-07
 */
@Controller
@RequestMapping("/system/product")
public class AProductController extends BaseController
{
    private String prefix = "system/product";
    @Resource
    private ISysRoleService sysRoleService;

    @Autowired
    private IAProductService aProductService;

    @Resource
    private ICategoryService categoryService;

    @Resource
    private ISysUserService sysUserService;

    @RequiresPermissions("system:product:view")
    @GetMapping()
    public String product()
    {
        return prefix + "/product";
    }

    /**
     * 查询商品列表
     */
    @RequiresPermissions("system:product:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(AProduct aProduct)
    {
        startPage();
        Long userId = ShiroUtils.getSysUser().getUserId();
        if (!isAdmin(userId)){
            aProduct.setOwner(userId);
        }
        List<AProduct> list = aProductService.selectAProductList(aProduct);
        List<ProductVo> productVos = new ArrayList<>();
        for (AProduct product:list) {
            ProductVo productVo = new ProductVo();
            BeanUtils.copyProperties(product,productVo);
            productVo.setCategory(categoryService.selectCategoryById(product.getCategoryId()).getName());
            String loginName = sysUserService.selectUserById(product.getOwner()).getUserName();
            productVo.setOwner(loginName);
            productVos.add(productVo);
        }
        return getDataTable(productVos);
    }

    /**
     * 查询商品列表
     * @param aProduct
     * @return
     */
    @PostMapping("/listV2")
    @ResponseBody
    public TableDataInfo listV2(AProduct aProduct)
    {
        startPage();
        List<AProduct> list = aProductService.selectAProductList(aProduct);
        List<ProductVo> productVos = new ArrayList<>();
        for (AProduct product:list) {
            ProductVo productVo = new ProductVo();
            BeanUtils.copyProperties(product,productVo);
            productVo.setCategory(categoryService.selectCategoryById(product.getCategoryId()).getName());
            String loginName = sysUserService.selectUserById(product.getOwner()).getUserName();
            productVo.setOwner(loginName);
            productVos.add(productVo);
        }
        return getDataTable(productVos);
    }
    /**
     * 导出商品列表
     */
    @RequiresPermissions("system:product:export")
    @Log(title = "商品", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(AProduct aProduct)
    {
        List<AProduct> list = aProductService.selectAProductList(aProduct);
        ExcelUtil<AProduct> util = new ExcelUtil<AProduct>(AProduct.class);
        return util.exportExcel(list, "商品数据");
    }

    /**
     * 新增商品
     */
    @GetMapping("/add")
    public String add(ModelMap mmap)
    {
        Category category = new Category();
        category.setStatus(0);
        mmap.put("categoryList", categoryService.selectCategoryList(category));
        return prefix + "/add";
    }

    /**
     * 新增保存商品
     */
    @RequiresPermissions("system:product:add")
    @Log(title = "商品", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(AProduct aProduct)
    {
//      补充物品主人
        aProduct.setOwner(ShiroUtils.getSysUser().getUserId());
        return toAjax(aProductService.insertAProduct(aProduct));
    }

    /**
     * 修改商品
     */
    @RequiresPermissions("system:product:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        AProduct aProduct = aProductService.selectAProductById(id);
        mmap.put("aProduct", aProduct);
        Category category = new Category();
        category.setStatus(0);
        mmap.put("categoryList", categoryService.selectCategoryList(category));
        return prefix + "/edit";
    }

    /**
     * 修改保存商品
     */
    @RequiresPermissions("system:product:edit")
    @Log(title = "商品", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(AProduct aProduct)
    {
        aProduct.setOwner(ShiroUtils.getSysUser().getUserId());
        return toAjax(aProductService.updateAProduct(aProduct));
    }

    /**
     * 删除商品
     */
    @RequiresPermissions("system:product:remove")
    @Log(title = "商品", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(aProductService.softDeleteAProductByIds(ids));
    }
}
